﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using WPFDevelopers.Samples.Models;
using WPFDevelopers.Samples.PageViews;

namespace WPFDevelopers.Samples.ViewModels
{
    public class SystemDicModel : ViewModelBase
    {
        public ObservableCollection<SystemDicM> systemDicObservable = new ObservableCollection<SystemDicM>();



        public List<SystemDicM> SumList = new List<SystemDicM>();
        public ObservableCollection<SystemDicM> resultData = new ObservableCollection<SystemDicM>();
        private PublicParam _querData;
        private PageSizeMode pageSizeMode;
        public PublicParam QuerData
        {
            get { return _querData; }
            set { _querData = value; this.NotifyPropertyChange("QuerData"); }
        }

        public PageSizeMode PageSizeMode
        {
            get { return pageSizeMode; }
            set { pageSizeMode = value; this.NotifyPropertyChange("PageSizeMode"); }
        }

        private void CurrentPageChanged(PublicParam param)
        {

            var skip = ((QuerData.PageIndex == 0 ? 1 : QuerData.PageIndex) - 1) * (QuerData.PageSize == 0 ? 14 : QuerData.PageSize);
            var takeCount = QuerData.PageSize == 0 ? 14 : QuerData.PageSize;
            resultData.Clear();
            resultData.AddRange(new ObservableCollection<SystemDicM>(SumList.Skip(skip).Take(takeCount).ToList()));
        }

        public int TableHight { get; set; }
        public int RowHight { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SystemDicModel()
        {
            for (int i = 0; i < 100; i++)
            {
                SystemDicM dicM = new SystemDicM();
                dicM.Id = i;
                dicM.PId = i;
                dicM.Name = "会员类型";
                dicM.Reamrk = "会员类型";
                dicM.IsEnable = 0;
                SumList.Add(dicM);
            }
            PageSizeMode = new PageSizeMode();
            QuerData = new PublicParam();
            QuerData.LoadDataEventHandler += CurrentPageChanged;
            QuerData.PageIndex = 1;
            QuerData.PageSize = 14;
            QuerData.SumCount = 100;
            PageSizeMode.PageHight = App.mainWindows.Height - 70;
            QuerData.TableHight = PageSizeMode.PageHight - 140;
            App.mainWindows.ChangeNum += MainWindows_ChangeNum;
            this.AddOrEditCommand = new RelayCommand<SystemDicM>(AddorEditClient);
            this.CheckAllDataClick = new RelayCommand(CheckAllData);
            this.DelCommand = new RelayCommand<SystemDicM>(Del);
        }



        #region 定义事件

        private void MainWindows_ChangeNum(int DataType)
        {
            /******
             * 获取屏幕的高度
             * 根据屏幕高度获取内容区域的高度
             * 内容区域高度-搜索框高度-分页高度=表格高度
             * 根据表格高度/每页的条数=每行的高度
             * 
             * *******/


            PageSizeMode.PageHight = App.mainWindows.Height - 70;
            QuerData.TableHight = PageSizeMode.PageHight - 140;
        }

        /// <summary>
        /// 添加或编辑窗体
        /// </summary>
        public RelayCommand<SystemDicM> AddOrEditCommand { get; private set; }

        public RelayCommand<SystemDicM> DelCommand { get; private set; }


        public RelayCommand CheckAllDataClick { get; }
        /// <summary>
        /// 添加或编辑窗体
        /// </summary>
        /// <param name="obj"></param>
        private void AddorEditClient(SystemDicM obj)
        {
            EditSystemDic systemDicWind = new EditSystemDic(obj);
            if (obj == null)
                systemDicWind.Title = "添加系统字典";
            else
                systemDicWind.Title = "编辑系统字典:" + obj.Name;
            systemDicWind.ShowDialog();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Del(SystemDicM obj)
        {
            // 单个删除
            if (obj != null)
            {
                resultData.Remove(obj);
            }
            else
            {
                // 批量删除
                var delList = resultData.Where(a => a.IsCheck == true).ToList();
                foreach (var item in delList)
                {
                    resultData.Remove(item);
                }

            }

        }

        /// <summary>
        /// 全选
        /// </summary>
        private void CheckAllData()
        {
            resultData.ForEach(a =>
            {
                if ((bool)a.IsCheck)
                    a.IsCheck = false;
                else
                    a.IsCheck = true;
            });
        }

        #endregion



    }
}
